﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Position.CircleTracker;
using WebcamCircleTrackerPositionSource;

namespace WebcamCircleTrackerPositionSourceConfiguration
{
    public partial class CircleTrackerConfigUC : UserControl, IImageDisplay
    {


        private CirclePositionSource circlePositionSource;

        public CirclePositionSource CirclePositionSource
        {
            get { return circlePositionSource; }
            set { circlePositionSource = value; }
        }

        public CircleTrackerConfigUC()
        {
            InitializeComponent();

            this.searchWindow = new Rectangle(0, 0, this.pbFiltered.Width, this.pbFiltered.Height);
        }

        private delegate void SetLabelDelegate(Label lbl, string text);

        private void SetLabelText(Label lbl, string text)
        {
            if (lbl.InvokeRequired)
                lbl.Invoke(new SetLabelDelegate(SetLabelText), lbl, text);
            else
                lbl.Text = text;

        }
        private void tbThreshold_Scroll(object sender, EventArgs e)
        {
            this.circlePositionSource.Threshold = (byte)tbThreshold.Value;
            SetLabelText(this.lblThreshold, "Threshold: " + this.circlePositionSource.Threshold.ToString());
        }


        private void tbMinBlobSize_Scroll(object sender, EventArgs e)
        {
            this.circlePositionSource.MinBlobSize = tbMinBlobSize.Value;
            SetLabelText(this.lblMinBlackCircleSize, "Min circle size: " + this.circlePositionSource.MinBlobSize.ToString());
        }

        private void tbMaxBlobSize_Scroll(object sender, EventArgs e)
        {
            this.circlePositionSource.MaxBlobSize = tbMaxBlobSize.Value;
            SetLabelText(this.lblMaxBlackCircleSize, "Max circle size: " + this.circlePositionSource.MaxBlobSize.ToString());

        }

        private void btnSetOverlayImage_Click(object sender, EventArgs e)
        {
            this.circlePositionSource.OverlayImage = txtOverlayPath.Text.Trim();
        }

        private delegate void SetImageDelegate(PictureBox pb, Bitmap image);

        public void SetImage(PictureBox pb, Bitmap image)
        {
            if (pb.InvokeRequired)
                pb.Invoke(new SetImageDelegate(SetImage), pb, image);
            else
            {
                try     //Try to draw the search windows
                {
                    Graphics g = Graphics.FromImage(image);
                    //Rectangle scaledSW = new Rectangle(
                    //    (int)(this.searchWindow.X * this.pbFiltered.Width / 640.0 ),
                    //    (int)(this.searchWindow.Y * this.pbFiltered.Height /480.0 ),
                    //    (int)(this.searchWindow.Width * this.pbFiltered.Width / 640.0),
                    //    (int)(this.searchWindow.Height * this.pbFiltered.Height / 480.0));

                    g.DrawRectangle(Pens.Blue, this.searchWindow);
                    g.DrawRectangle(Pens.Red, this.realSearchWindow);

                    g.Dispose();
                }
                catch { }
                pb.Image = image;
            }
        }



        #region IImageDisplay Members

        public void DisplayImage(Bitmap[] image)
        {
            if (this.selectedIndex >= 0 && this.selectedIndex <=3)
                SetImage(this.pbFiltered, image[this.selectedIndex]);
        }

        #endregion

        private int selectedIndex = 0;

        private void lbFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.selectedIndex = lbFilter.SelectedIndex;
        }

        private void btnSetSearchWindow_Click(object sender, EventArgs e)
        {


            this.circlePositionSource.SearchWindow = this.searchWindow;
        }

        #region IImageDisplay Members

        private Rectangle realSearchWindow = new Rectangle(0, 0, 640, 480);

        public void SetRealSearchWindow(Rectangle realSearchWindow)
        {
            this.realSearchWindow = realSearchWindow;
        }

        #endregion

        private Rectangle searchWindow;


        private void tbWindow_ValueChanged(object sender, EventArgs e)
        {
            this.searchWindow = new Rectangle(this.tbWindowX.Value, 480 - this.tbWindowYInv.Value,
                this.tbWindowSize.Value,
                this.tbWindowSize.Value);

        }

        private void cbLockWindow_CheckedChanged(object sender, EventArgs e)
        {
            this.circlePositionSource.LockSearchWindow = cbLockWindow.Checked;
        }
    }
}
